import { get, post, put, del, merchantService } from './request'
import type { ApiResponse } from './user'

// 商家相关接口类型定义
export interface Merchant {
  id: number
  username: string
  name: string
  address: string
  phone: string
  description: string
  image: string
  businessHours: string
  deliveryFee: number
  minOrderAmount: number
  categoryId: number
  status: number
  rating?: number
}

export interface MerchantCategory {
  id: number
  name: string
  icon: string
  sort: number
  status: number
}

export interface MerchantReview {
  id: number
  merchantId: number
  userId: number
  orderId: number
  content: string
  rating: number
  images: string
  status: number
  reply?: string
  createTime: string
}

export interface MerchantLoginParams {
  username: string
  password: string
}

export interface MerchantRegisterParams {
  username: string
  password: string
  name: string
  address: string
  phone: string
  description: string
  image?: string
  businessHours: string
  deliveryFee: number
  minOrderAmount: number
  categoryId: number
  status?: number
}

// 商家注册
export function registerMerchant(data: MerchantRegisterParams) {
  return post<ApiResponse<Merchant>>(merchantService, '/merchants/register', data)
}

// 商家登录
export function loginMerchant(data: MerchantLoginParams) {
  return post<ApiResponse<string>>(merchantService, '/merchants/login', data)
}

// 获取当前登录商家信息
export function getCurrentMerchant() {
  return get<ApiResponse<Merchant>>(merchantService, '/merchants/current')
}

// 更新商家信息
export function updateMerchant(id: number, data: Partial<Merchant>) {
  return put<ApiResponse<Merchant>>(merchantService, `/merchants/${id}`, data)
}

// 更新商家状态
export function updateMerchantStatus(id: number, status: number) {
  return put<ApiResponse<Merchant>>(merchantService, `/merchants/${id}/status?status=${status}`)
}

// 获取推荐商家列表
export function getRecommendedMerchants() {
  return get<ApiResponse<Merchant[]>>(merchantService, '/merchants/recommended')
}

// 根据分类ID查询商家列表
export function getMerchantsByCategory(categoryId: number, page: number = 0, size: number = 10) {
  return get<ApiResponse<{
    content: Merchant[]
    totalElements: number
    totalPages: number
  }>>(merchantService, `/merchants/category/${categoryId}`, { page, size })
}

// 根据商家名称模糊查询
export function searchMerchants(name: string, page: number = 0, size: number = 10) {
  return get<ApiResponse<{
    content: Merchant[]
    totalElements: number
    totalPages: number
  }>>(merchantService, '/merchants/search', { name, page, size })
}

// 获取商家信息
export function getMerchantById(id: number) {
  return get<ApiResponse<Merchant>>(merchantService, `/merchants/${id}`)
}

// 获取所有商家分类
export function getAllMerchantCategories() {
  return get<ApiResponse<MerchantCategory[]>>(merchantService, '/merchant-categories')
}

// 根据商家ID查询评价列表
export function getMerchantReviews(merchantId: number) {
  return get<ApiResponse<MerchantReview[]>>(merchantService, `/merchant-reviews/merchant/${merchantId}`)
}

// 计算商家的平均评分
export function getMerchantRating(merchantId: number) {
  return get<ApiResponse<{ rating: number }>>(merchantService, `/merchant-reviews/merchant/${merchantId}/rating`)
}

// 商家回复评价
export function replyToReview(id: number, reply: string) {
  return put<ApiResponse<MerchantReview>>(merchantService, `/merchant-reviews/${id}/reply?reply=${encodeURIComponent(reply)}`)
}

// 创建商家评价
export function createMerchantReview(data: Partial<MerchantReview>) {
  return post<ApiResponse<MerchantReview>>(merchantService, '/merchant-reviews', data)
} 